package com.wangmei.service.serviceimpl;

import com.wangmei.mapper.AttributevalueMapper;
import com.wangmei.mapper.CategeryMapper;
import com.wangmei.mapper.ProductMapper;
import com.wangmei.mapper.ProductkuidMapper;
import com.wangmei.model.Attributevalue;
import com.wangmei.model.Product;
import com.wangmei.model.ProductExample;
import com.wangmei.model.Productkuid;
import com.wangmei.pojo.ProductCollection;
import com.wangmei.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private AttributevalueMapper attributeViewMapper;
    @Autowired
    private ProductkuidMapper productkuidMapper;

    @Override
    public List<Product> getProductList() {
        return productMapper.selectAll();
    }

    @Override
    public Product getProductByPid(int pid) {
        return productMapper.selectByPrimaryKey(pid);
    }


    @Override
    @Transactional
    public int addProduct(Product product) {
        productMapper.insertSelective(product);
        //获取pid
        int pid = product.getPid();
        //查询vidlist
        List<Attributevalue> attributevaluesList =
                attributeViewMapper.getAttributevalueByCid(product.getCid().toString());
        //插入到kuid表
        Productkuid record;
        for(Attributevalue av :attributevaluesList){
            record = new Productkuid();
            record.setPid(pid);
            record.setVid(av.getVid());
            record.setStorenum(0);
            productkuidMapper.insertSelective(record);
        }
        return 1;
    }

    @Override
    public int updateProduct(Product product) {
        return productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public int deleteProduct(int pid) {
        return productMapper.deleteByPrimaryKey(pid);
    }

    @Override
    public int deleteProducts(List<Integer> pids) {
        return productMapper.deleteByPids(pids);
    }

    @Override
    public List<Product> getProductByPname(String pname) {
        return productMapper.selectProductByPname(pname);
    }

    @Override
    public List<Product> selectByExample(ProductExample example) {
        return productMapper.selectByExample(example);
    }

    @Override
    public List<ProductCollection> getProductByCid(Map<String,Object> map) {
        return productMapper.getProductByCid(map);
    }

}
